package com.tpshion.cloud.core.exception;

import com.tpshion.cloud.common.support.CoreException;
import com.tpshion.cloud.common.support.ErrorCode;
import com.tpshion.cloud.common.support.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 核心统一异常处理
 */
@RestControllerAdvice
public class CoreExceptionAdvice {

    private static final Logger log = LoggerFactory.getLogger(CoreExceptionAdvice.class);

    /**
     * 处理系统异常
     */
    @ExceptionHandler(Exception.class)
    public Result systemException(Exception e){
        Result result = Result.build(ErrorCode.SYSTEM_ERROR);

        if(e instanceof BindException){
            // 请求参数校验异常
            BindException exception = (BindException) e;
            ObjectError objectError = exception.getAllErrors().get(0);
            result = Result.fail(ErrorCode.PARAM_ERROR.getCode(),objectError.getDefaultMessage());
            log.info("paramException: errorMsg={}",exception.getFieldError());
        }else if(e instanceof HttpMessageNotReadableException){
            // 请求体参数为空异常
            HttpMessageNotReadableException ex = (HttpMessageNotReadableException) e;
            result = Result.build(ErrorCode.REQUEST_BODY_NULL);
            log.info("HttpMessageNotReadableException: errorMsg={}",ex.getMessage());
        }else if(e instanceof HttpMediaTypeNotSupportedException){
            // Content-type类型错误
            result = Result.fail(ErrorCode.CONTENT_TYPE_ERROR.getCode(),e.getMessage());
            log.info("HttpMediaTypeNotSupportedException: errorMsg={}",e.getMessage());
        }else if(e instanceof MethodArgumentNotValidException){
            // 请求参数校验异常
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            FieldError fieldError = ex.getBindingResult().getFieldErrors().get(0);
            result = Result.fail(ErrorCode.PARAM_ERROR.getCode(),fieldError.getDefaultMessage());
            log.info("paramException: errorMsg={}",ex.getMessage());
        } else if(e instanceof MaxUploadSizeExceededException){
            //上传文件超限制异常
            MaxUploadSizeExceededException ex = (MaxUploadSizeExceededException) e;
            log.info("file upload size exception:{}",e.getMessage());
            result = Result.build(ErrorCode.FILE_TOO_BIG);
        } else{
            log.error("systemException: errorMsg={}",e.getMessage(),e);
        }

        return result;
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(CoreException.class)
    public Result coreException(Exception e){
        if(e instanceof CoreException){
            CoreException coreException = (CoreException) e;
            log.info("coreException: errorCode={},errorMsg={}",coreException.getCode(),coreException.getMsg());
            return Result.fail(coreException.getCode(),coreException.getMsg());
        }else {
            log.error("Exception: errorMsg={}",e.getMessage(),e);
            return Result.build(ErrorCode.SYSTEM_ERROR);
        }
    }

}
